package com.wxapp.admin.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wxapp.admin.api.pojo.entity.CompanyDO;
import com.wxapp.admin.api.pojo.entity.DeptJobDO;
import com.wxapp.admin.api.pojo.entity.JobDO;
import com.wxapp.admin.api.pojo.entity.UserCDJDO;
import com.wxapp.admin.api.pojo.vm.userCDJ.PCompanySelectVm;
import com.wxapp.admin.api.pojo.vm.userCDJ.PJobSelectVm;
import com.wxapp.admin.api.pojo.vm.userCDJ.PUserSelectVm;
import com.wxapp.admin.api.pojo.vo.userDeptJob.PAddUserDeptJobVo;
import com.wxapp.admin.service.mapper.*;
import com.wxapp.admin.service.service.UserCDJService;
import com.wxapp.common.constant.base.IBaseService;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.constant.pojo.vo.UserVo;
import com.wxapp.common.util.check.CheckUtil;
import com.wxapp.common.util.copy.CopyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lgl
 * @Description
 * @Datetime 2022/8/13 18:55
 * @Modified By
 */
@Service
@RequiredArgsConstructor
public class UserCDJServiceImpl extends IBaseService implements UserCDJService {
    private final JobMapper jobMapper;
    private final DeptJobMapper deptJobMapper;
    private final UserCDJMapper userCDJMapper;
    private final  CompanyMapper companyMapper;

    @Override
    public ResultVm<List<PJobSelectVm>> selectJob(String name, String deptId) {
        List<DeptJobDO> deptJobDOList = null;
        if (CheckUtil.isNotNullObject(deptId)) {
            LambdaQueryWrapper<DeptJobDO> deptJobQuery = new LambdaQueryWrapper<>();
            deptJobQuery.select(DeptJobDO::getJobId).eq(DeptJobDO::getDeptId, deptId);
            deptJobDOList = deptJobMapper.selectList(deptJobQuery);
        }
        LambdaQueryWrapper<JobDO> query = new LambdaQueryWrapper<>();
        query.select(JobDO::getId, JobDO::getName);
        if (CheckUtil.isNotNullList(deptJobDOList)) {
            query.in(JobDO::getId, deptJobDOList.stream().map(DeptJobDO::getJobId).collect(Collectors.toList()));
        }
        query.like(CheckUtil.isNotNullString(name), JobDO::getName, name).eq(JobDO::getStatus, true);
        var roleList = jobMapper.selectList(query);
        return success(CopyUtil.copyListNew(roleList, PJobSelectVm::new, null));
    }

    @Override
    public ResultVm<String> addUserDeptJob(UserVo userVo, PAddUserDeptJobVo pAddUserDeptJobVo) {
        var userId = pAddUserDeptJobVo.getUserId();
        var deptId = pAddUserDeptJobVo.getDeptId();
        LambdaUpdateWrapper<UserCDJDO> query = new LambdaUpdateWrapper<>();
        query.eq(UserCDJDO::getUserId, userId).eq(UserCDJDO::getDeptId, deptId);
        userCDJMapper.delete(query);
        List<UserCDJDO> insertData = new ArrayList<>();
        if (CheckUtil.isNotNullList(pAddUserDeptJobVo.getJobIds())) {
            pAddUserDeptJobVo.getJobIds().forEach(s -> {
                UserCDJDO userCDJDO = new UserCDJDO();
                userCDJDO.setUserId(userId);
                userCDJDO.setDeptId(deptId);
                userCDJDO.setJobId(s);
                insertData.add(userCDJDO);
            });
        } else {
            UserCDJDO userCDJDO = new UserCDJDO();
            userCDJDO.setUserId(userId);
            userCDJDO.setDeptId(deptId);
            insertData.add(userCDJDO);
        }
        userCDJMapper.insertBatch(insertData);
        return success();
    }

    @Override
    public ResultVm<List<PCompanySelectVm>> getCompanyList(String name) {
        LambdaQueryWrapper<CompanyDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CompanyDO::getStatus, true).like(CheckUtil.isNotNullString(name), CompanyDO::getName, name).select(CompanyDO::getId, CompanyDO::getName);
        var companyDOS = companyMapper.selectList(wrapper);
        return success(CopyUtil.copyListNew(companyDOS, PCompanySelectVm::new, null));
    }

    @Override
    public ResultVm<List<PUserSelectVm>> userSelect(String companyId) {
        List<PUserSelectVm> userSelectVms = userCDJMapper.userSelect(companyId);
        return success(userSelectVms);
    }
}
